#include "FileDataProvider.h"

namespace simple_renderer{

FileDataProvider::FileDataProvider() : _yuv_reader(nullptr),
                                       _count(0){

}

FileDataProvider::~FileDataProvider(){

}

int FileDataProvider::InitProvider(const char* file_name){
    if(_yuv_reader != nullptr){
        _yuv_reader = nullptr;
    }
    _yuv_reader.reset(new YUVReader);
    int ret = _yuv_reader->InitReader(file_name);
    return ret;
}

int FileDataProvider::ProvideNextFrame(){

    uint8_t *data = _yuv_reader->NextFrame(_count);
    if (data == nullptr) {
        _count = 0;
        data = _yuv_reader->NextFrame(_count);
    }
    _count++;
    if (_frame_renderer != nullptr) {
        int w = _yuv_reader->width;
        int h = _yuv_reader->height;
        int frame_len = _yuv_reader->frame_size;
        FrameType frame_type = kI420P;

        LOGI("[FileDataProvider] ProvideNextFrame w=%d h=%d frame_len=%d frame_type=%d", w, h,
             frame_len, frame_type);
        _frame_renderer->DrawFrame(w, h, data, frame_len, frame_type);
        delete[] data;
    }

    return 0;
}

int FileDataProvider::DeinitProvider(){
    _yuv_reader = nullptr;
    _count = 0;
    return 0;
}

}